﻿using System;
using System.Collections.Generic;
using System.Text;
 
using AppToolKit.Core.Context.Spring;
using Model.TransfersConfig;
using System.IO;
using Utility;
using System.Reflection;
using Common.Logging;
using BLL.Action.Transfer.WebService;
using BLL.Action.Core.Caching;

namespace BLL.Action.Transfer
{
    public class AuthorParser
    {
       static ILog log = LogManager.GetLogger(LogDefault.AppDebug);

       static string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, string.Format("Config{0}WSAuthor{0}", Path.DirectorySeparatorChar));
        [AutoWiring]
        public ClientCache Cache { get; set; }

        private  List<Author> GetAuthorList()
        {
            string file = string.Format("{0}{1}", path, "Author.xml");


            List<Author> List = CacheHelper.Cache(Cache,file,delegate{return  XmlReadWriteHelper.Deserialize(typeof(List<Author>), file);}) as List<Author>;

            return List;
        }
        public object GetSoapHeader(Type type)
        {
            return CacheHelper.Cache(Cache, type.FullName, delegate { return SetHeader(type); });
        }
        public object SetHeader(Type type)
        {
            try
            {
                 
                List<Author> List = GetAuthorList();
                foreach (Author author in List)
                {
                    Type t = Type.GetType(author.ClassName);
                    if (t.FullName == type.FullName)
                    {
                        Type t1 = Type.GetType(author.Header.SoapHeaderName);

                        object header = Activator.CreateInstance(t1);

                        PropertyInfo Property_User = t1.GetProperty(author.Header.UserFiled);
                        PropertyInfo Property_PassWord = t1.GetProperty(author.Header.PassWordFiled);

                        Property_PassWord.SetValue(header, author.Header.PassWord, null);
                        Property_User.SetValue(header, author.Header.User, null);


                        return header;
                       

                    }
                }
                 
            }
            catch (Exception ex)
            {
                log.Debug(ex);                 

            }
            return null;
        }
    }
}
